from typing import Callable


from ctaf_core.ctaf_objects.base_bundle import BaseBundle
from ctaf_core.ctaf_objects.base_event import MsgEvent
from ctaf_core.ctaf_objects.base_service_request import BaseServiceReq
from ctaf_core.ctaf_objects.base_service_response import BaseServiceResp
from ctaf_core.ctaf_resources.ctaf_settings import CTAFsettings
from ctaf_core.log_trace_bundle.logging import LogRecorder, LoggerFactory
from ctaf_core.bus_client_bundle.bus_service_client import BusServiceClient


class BusClientBundle(BaseBundle):
    """
    """
    log_recorder: LogRecorder = LoggerFactory.get_logger()

    def __init__(self) -> None:
        super().__init__("BusClientBundle")
        self.bus_service_client_setting_filename: str = CTAFsettings.get_setting("busServiceClientSettingFilename")
        self.client: BusServiceClient = BusServiceClient(self.bus_service_client_setting_filename)

    def load_bundle(self):
        self.client.start()

    #重定义bus client收到bus server消息之后的回调处理逻辑，此处将收到消息放入本地事件驱动引擎，后续由本地事件处理器处理
    def reg_recv_msg_handler(self, recv_msg_process: Callable[[MsgEvent], None]):
        self.client.callback = recv_msg_process

    def send_rpc_request(self, req: BaseServiceReq) -> BaseServiceResp:

        rpc_service_resp: BaseServiceResp = self.client.invoke_rpc_request(req)
        return rpc_service_resp

    def unload_bundle(self):
        self.client.stop()
